﻿using System;
using System.Data;
using System.Data.Common;

/// <summary>
/// Summary description for EF_User
/// </summary>
public static class EF_User
{
	private static DataTable table;
	static EF_User()
	{
		//
		// TODO: Add constructor logic here
		//
	}

	/// <summary>
	/// Create new user
	/// </summary>
	/// <param name="user">Collection of User properties</param>
	public static void InsertUser(EF_UserProperty user)
	{
		if (!checkUniqueUsername(user.Username))
		{
			DbCommand cmd = GenericDataAccess.CreateCommand();
			cmd.CommandText = "EF_InsertUser";

			DbParameter UserParam = cmd.CreateParameter();
			UserParam.ParameterName = "@RoleID";
			UserParam.Value = user.RoleID;
			UserParam.DbType = DbType.Int32;
			cmd.Parameters.Add(UserParam);

			UserParam = cmd.CreateParameter();
			UserParam.ParameterName = "@FirstName";
			UserParam.Value = user.FirstName;
			UserParam.DbType = DbType.String;
			cmd.Parameters.Add(UserParam);

			UserParam = cmd.CreateParameter();
			UserParam.ParameterName = "@LastName";
			UserParam.Value = user.LastName;
			UserParam.DbType = DbType.String;
			cmd.Parameters.Add(UserParam);

			UserParam = cmd.CreateParameter();
			UserParam.ParameterName = "@Username";
			UserParam.Value = user.Username;
			UserParam.DbType = DbType.String;
			cmd.Parameters.Add(UserParam);

			UserParam = cmd.CreateParameter();
			UserParam.ParameterName = "@Password";
			UserParam.Value = user.Password;
			UserParam.DbType = DbType.String;
			cmd.Parameters.Add(UserParam);

			GenericDataAccess.ExecuteCommand(cmd);
		}
		else
		{
			// Email is already existed!
		}
	}

	public static DataTable SelectUser(Int32 id = 0)
	{
		DbCommand cmd = GenericDataAccess.CreateCommand();
		cmd.CommandText = "EF_SelectUser";

		DbParameter UserParam = cmd.CreateParameter();
		UserParam.ParameterName = "@UserID";
		UserParam.Value = id;
		UserParam.DbType = DbType.Int32;
		cmd.Parameters.Add(UserParam);

		table = new DataTable();
		table = GenericDataAccess.ExecuteSelectCommand(cmd);

		return table;
	}

	/// <summary>
	/// Check whether the email is already existed or not
	/// </summary>
	/// <param name="email">New user email</param>
	/// <returns>Boolean</returns>
	public static bool checkUniqueUsername(string username)
	{
		DbCommand cmd = GenericDataAccess.CreateCommand();
		cmd.CommandText = "EF_CheckUsernameUser";

		DbParameter UserParam = cmd.CreateParameter();
		UserParam.ParameterName = "@Username";
		UserParam.Value = username;
		UserParam.DbType = DbType.String;
		cmd.Parameters.Add(UserParam);

		table = new DataTable();
		table = GenericDataAccess.ExecuteSelectCommand(cmd);
		if (table.Rows.Count > 0)
			return true;
		return false;
	}
}